package m.jclecai.com.activity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.Response.ResponseSignList;
import m.jclecai.com.bean.UserBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.listener.Callback;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.view.AutoSwipeRefreshLayout;

public class GameRoomActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener{

    @BindView(R.id.tv_my_gold_number)
    TextView mTvMyGoldNumber;

    @BindView(R.id.ll_guess)
    LinearLayout mLlGuess;

    @BindView(R.id.ll_exchange)
    LinearLayout mLlExchange;

    @BindView(R.id.ll_task)
    LinearLayout mLlTask;

    @BindView(R.id.ll_top)
    LinearLayout mLlTop;

    @BindView(R.id.iv_game_item3)
    ImageView mIvGameItem3;

    @BindView(R.id.iv_game_item4)
    ImageView mIvGameItem4;

    @BindView(R.id.ll_bottom_items)
    LinearLayout mLlBottomItems;

    @BindView(R.id.iv_game_item1)
    ImageView mIvGameItem1;

    @BindView(R.id.iv_game_item2)
    ImageView mIvGameItem2;

    @BindView(R.id.rl_refresh_layout)
    AutoSwipeRefreshLayout mRlRefreshLayout;


    private UserBean mUserBean;

    @Override
    protected int getContentView() {
        return R.layout.activity_game_room;
    }

    @Override
    protected void initView() {
        setTitle("娱乐厅");
        leftOptionText("");
        mTvMyGoldNumber.setText("0");
    }

    @OnClick(R.id.ll_my_money)
    public void onClickMyMoney(){
        startActivity(GoldManagerActivity.getGoldManagerIntent(this,true));
    }

    @Override
    protected void initData() {
        initUser();
        requestMyGoldNumber();
        mRlRefreshLayout.setOnRefreshListener(this);
        mRlRefreshLayout.setRefreshing(true);
    }

    private void initUser() {
        mUserBean = PreferenceUtil.readUser(this);
    }

    /**
     * 请求服务器获取金币；
     */
    private void requestMyGoldNumber() {
        //请求服务器获取金币的数量

        String userName = PreferenceUtil.getUserName(this);
        String password = PreferenceUtil.getPassword(this);
        HashMap<String, String> pair = new HashMap<>();
        pair.put("user", userName);
        pair.put("password", password);
        HttpManager.postAsyn(Constant_Url.SIGN_LIST, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {

                if (mRlRefreshLayout != null)
                    mRlRefreshLayout.setRefreshing(false);
                DisPlay("请检查网络！！！");
            }

            @Override
            public void onResponse(String response) {
                if (mRlRefreshLayout != null)
                    mRlRefreshLayout.setRefreshing(false);

                ResponseSignList responseSignList = FastOk.get(response, ResponseSignList.class);

                if ("0".equals(responseSignList.getStatus())) {
                    //这里我们要做的一个操作就是更新列表签到的列表；
                    mTvMyGoldNumber.setText(TextUtils.isEmpty(responseSignList.getGoldcoin())?"0":responseSignList.getGoldcoin());
                }else{
                    DisPlay(responseSignList.getMessage());
                }
            }
        }, pair);
    }

    @Override
    protected void initListener() {
        mLlGuess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //竞猜
                DisPlay("敬请期待！！！");
            }
        });

        mLlExchange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //只有登陆了,对金币进一个管理
            checkLogin(new Callback(){
                @Override
                public void callbackSuccess() {
                    startActivity(new Intent(GameRoomActivity.this, GoldManagerActivity.class));
                }
            });
            }
        });


        mLlTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //任务
                startActivity(new Intent(GameRoomActivity.this, TaskActivity.class));
            }
        });

        mIvGameItem1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //大转盘====>抽奖
                checkLogin(new Callback(){
                    @Override
                    public void callbackSuccess() {
                        super.callbackSuccess();
                        startActivity(new Intent(GameRoomActivity.this, LuckyWalkActivity.class));
                    }
                });
            }
        });
        mIvGameItem2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //金币足球
                DisPlay("敬请期待！！！");
            }
        });
        mIvGameItem3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //金币11选5
                DisPlay("敬请期待！！！");
            }
        });
        mIvGameItem4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //加金币双色球
                DisPlay("敬请期待！！！");
            }
        });
    }

    @Override
    public void onRefresh() {
        requestMyGoldNumber();
    }
}
